import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;

/**
 * An actor class that can display a scoreboard, using Greenfoot's
 * UserInfo class.  
 * 
 * You typically use this by including some code into the world for when your game ends:
 * 
 * <pre>
 *   addObject(new ScoreBoard(800, 600), getWidth() / 2, getHeight() / 2); 
 * </pre>
 * 
 * Where 800 by 600 should be replaced by the desired size of the score board. 
 * 
 * @author Neil Brown 
 * @version 1.0
 */
public class ScoreBoard extends Actor
{
    /**
     * Constructor for objects of class ScoreBoard.
     * <p>
     * You can specify the width and height that the score board should be, but
     * a minimum width of 600 will be enforced.
     */
    public ScoreBoard(int width, int height)
    {    
        setImage(new GreenfootImage(width, height)); 
        
        drawScores();
    }
    
        
    private void drawScores()
    {   
        getImage().setColor(new Color(0,0,0,0));
        getImage().fill();

        drawPanel(UserInfo.getTop(5));
        drawUserPanel();
    }
    
    private void drawPanel(List users)
    {    
        if (users == null)
        return;
        
        for (int i=0,y=77;i<users.size();i++,y+=59)
        {
            UserInfo playerData = (UserInfo)users.get(i); 
            
            int score=playerData.getScore()*-1;
            int minutes=score/60;
            int seconds=score%60;
            
            getImage().drawImage(playerData.getUserImage(), 63, y);
            drawMessage(minutes+"' "+seconds+"''", 160, y-5,20);
            drawMessage(playerData.getUserName(), 160, y+20,20);
        }
    }
    
    private void drawUserPanel()
    {
        if(UserInfo.isStorageAvailable())
        {
            UserInfo myInfo=UserInfo.getMyInfo();
            
            if(myInfo!=null)
            {
                int score=myInfo.getScore()*-1;
                int rank=myInfo.getRank();
                int minutes=score/60;
                int seconds=score%60;
                
                if(score!=0)
                drawMessage(minutes+"' "+seconds+"''", 480, 245,30);
                else
                drawMessage("-", 480, 245,30);
                
                getImage().drawImage(myInfo.getUserImage(), 445, 177);
                
                if(rank!=-1)
                drawMessage(rank+"", 550, 180,40);
                else
                drawMessage("-", 550, 182,40);
            }
        }
    }
    
    public void drawMessage(String message,int x,int y,int fontSize)
    {
        GreenfootImage image=new GreenfootImage(message.length()*fontSize/2,fontSize+10);
        image.clear();
        image.setColor(new Color(0,0,0,0));
        image.fill();
        image.setColor(new Color(255,255,255));
        image.setFont(new java.awt.Font("Impact",java.awt.Font.PLAIN,fontSize));
        image.drawString(message,0,image.getHeight()/2+10);
        getImage().drawImage(image,x,y);
    }
}
